* This file takes CCES Data from 2018 and cleans it for use in analyzing the survey
* and first experiment in the Musgrave-Leal cyber project.


version 14
clear all
set more off

* change this to fit your directory structure
global path "~/Dropbox/0001 Academic Projects/Ongoing/0148 The Cyber/Posted/FPA Version/Work"
cd "$path/Raw/"
use "CCES18_UMB_STATA13Trimmed.dta"

/*

* This is a trimmed version of data from the 'B' module of the University of 
Massachusetts Amherst module on the 2018 CCES. Full dataset and codebook:
https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/HWGUH4&version=1.0

* this trimmed dataset keeps the following: 

keep caseid teamweight tookpost CCEStake birthyr gender educ race race_other ///
hispanic marstat multrace_1 multrace_2 multrace_3 multrace_4 multrace_5 ///
multrace_8 multrace_97 multrace_98 multrace_99 votereg CC18_300_1 CC18_300_2 ///
CC18_300_3 CC18_300_4 CC18_300_5 CC18_300_6 CC18_300b CC18_300c CC18_300d_1 ///
CC18_300d_2 CC18_300d_3 CC18_300d_4 CC18_300d_5 CC18_302 CC18_308a CC18_308b ///
CC18_308c CC18_308d CC18_308e CC18_309a CC18_309b CC18_309c CC18_309d ///
CC18_335 CC18_336 pid3 pid7 internetwork internethome newsint faminc_new ///
UMA306a UMA306b UMA306c UMA306d UMA306e UMA307a UMA307b UMA307c UMA307d ///
UMA307e UMA307f UMA307g UMA312 UMA324 UMA324treat UMB404 UMB405 UMB406 ///
UMB407 UMB408 UMB409 UMB410 UMB411 UMA312 UMB404treat

The code produces a datafile with a more useful and easily interpretable set of 
variables
*/

* college 1 if college degree or higher, 0 otherwise
gen college = .
replace college = 1 if educ >= 5
replace college = 0 if educ < 5


* 3-point Party ID score, with leaners coded as independents

gen dem3 = .
replace dem3 = 1 if pid3 == 1
replace dem3 = 0 if pid3 > 1

gen ind3 = .
replace ind3 = 1 if pid3 == 3
replace ind3 = 0 if pid3 < 3 | pid3 > 3

gen gop3 = .
replace gop3 = 1 if pid3 == 2
replace gop3 = 0 if pid3 == 1
replace gop3 = 0 if pid3 >= 3

* 7-point Party ID score, with leaners included as partisans

gen dem7 = .
gen ind7 = .
gen gop7 = .

replace dem7 = 1 if pid7 == 1 | pid7 == 2 | pid7 == 3
replace dem7 = 0 if pid7 >3 & pid7 != .

replace ind7 = 1 if pid7 == 4
replace ind7 = 0 if (pid7 < 4 | pid7 > 4) & pid7 !=.

replace gop7 = 1 if pid7 >= 5 & pid7 != .
replace gop7 = 0 if pid7 < 5 & pid7 != .

* white non-hispanic id
gen white = .
replace white = 1 if race == 1 & hispanic == 2
replace white = 0 if race != 1 | hispanic == 1

gen age = 2018-birthyr
gen agesq = age*age

* gender binary

gen female = .
replace female = 1 if gender == 2
replace female = 0 if gender == 1

* next, create variables to combine the DVs and produce indicator
* terms

* Generate variable to measure overall international cyber worry
* as a mean of the variables in UMB307*

* Note higher means higher overall worries

egen cyberworryintl = rowmean(UMA307a UMA307b UMA307c UMA307d UMA307e UMA307g) ///
	if !missing(UMA307a, UMA307b, UMA307c, UMA307d, UMA307e, UMA307g)

* Generate variable to measure sectoral cyber worry
* as a mean of the variables in UMA306*

* Note lower means higher overall worries about US vulnerability

egen cyberworrydomestic = rowmean(UMA306a UMA306b UMA306c UMA306d UMA306e) ///
	if !missing(UMA306a, UMA306b, UMA306c, UMA306d, UMA306e) 
	
* Generate personal de-meaned scores

gen threatrussia = 	UMA307a-cyberworryintl if UMA307a != . & cyberworryintl !=.
gen threatchina = 	UMA307b-cyberworryintl if UMA307b != . & cyberworryintl !=.
gen threatiran = 	UMA307c-cyberworryintl if UMA307c != . & cyberworryintl !=.
gen threatkorea = 	UMA307d-cyberworryintl if UMA307d != . & cyberworryintl !=.
gen threatcrime = 	UMA307e-cyberworryintl if UMA307e != . & cyberworryintl !=.
gen threatcanada = 	UMA307f-cyberworryintl if UMA307f != . & cyberworryintl !=.
gen threatterror = 	UMA307g-cyberworryintl if UMA307g != . & cyberworryintl !=.


* Generate dichotomous treatment variables

* endorsement cues
gen cueCIA = .
replace cueCIA = 1 if UMB404treat == 1 | UMB404treat == 2
replace cueCIA = 0 if UMB404treat > 2 & UMB404treat != .

gen cueAgree = .
replace cueAgree = 1 if UMB404treat == 1 | UMB404treat == 3
replace cueAgree = 0 if UMB404treat == 2 | UMB404treat == 4


* combining treatment condition DVs
gen confidenceiran = .
replace confidenceiran = UMB404 if UMB404 != .
replace confidenceiran = UMB406 if UMB406 != .
replace confidenceiran = UMB408 if UMB408 != .
replace confidenceiran = UMB410 if UMB410 != .

gen timetoattack = .
replace timetoattack = UMB405 if UMB405 != .
replace timetoattack = UMB407 if UMB407 != .
replace timetoattack = UMB409 if UMB409 != .
replace timetoattack = UMB411 if UMB411 != .

lab def timetoattacklab 1 "Would support" 2 "Would not support" 3 "Don't know"
lab val timetoattack timetoattacklab

* recoding experimental DV
* dichotomous version
gen timetoattackdichot = .
replace timetoattackdichot = 1 if timetoattack == 1
replace timetoattackdichot = 0 if timetoattack == 2 | timetoattack == 3
* dichotomous indicator for don't know
gen timetoattackdontknow = .
replace timetoattackdontknow = 1 if timetoattack == 3
replace timetoattackdontknow = 0 if timetoattack == 2 | timetoattack == 1

* generate news interest and flip scoring
gen newsintadj = .
replace newsintadj = 0 if newsint == 4
replace newsintadj = 1 if newsint == 3
replace newsintadj = 2 if newsint == 2
replace newsintadj = 3 if newsint == 1

lab def newsintadjlab 0 "Hardly at all" 1 "Only now and then" 2 "Some of the time" 3 "Most of the time"

lab val newsintadj newsintadjlab


gen vulnerablepower 	= .
gen vulnerablecorp 		= .
gen vulnerablemil 		= .
gen vulnerablefed 		= .
gen vulnerablevote 		= .

replace vulnerablepower = 4 if UMA306a == 1
replace vulnerablepower = 3 if UMA306a == 2
replace vulnerablepower = 2 if UMA306a == 3
replace vulnerablepower = 1 if UMA306a == 4
replace vulnerablepower = 0 if UMA306a == 5

replace vulnerablecorp = 4 if UMA306b == 1
replace vulnerablecorp = 3 if UMA306b == 2
replace vulnerablecorp = 2 if UMA306b == 3
replace vulnerablecorp = 1 if UMA306b == 4
replace vulnerablecorp = 0 if UMA306b == 5
 
replace vulnerablemil = 4 if UMA306c == 1
replace vulnerablemil = 3 if UMA306c == 2
replace vulnerablemil = 2 if UMA306c == 3
replace vulnerablemil = 1 if UMA306c == 4
replace vulnerablemil = 0 if UMA306c == 5
 
replace vulnerablefed = 4 if UMA306d == 1
replace vulnerablefed = 3 if UMA306d == 2
replace vulnerablefed = 2 if UMA306d == 3
replace vulnerablefed = 1 if UMA306d == 4
replace vulnerablefed = 0 if UMA306d == 5
 
replace vulnerablevote = 4 if UMA306e == 1
replace vulnerablevote = 3 if UMA306e == 2
replace vulnerablevote = 2 if UMA306e == 3
replace vulnerablevote = 1 if UMA306e == 4
replace vulnerablevote = 0 if UMA306e == 5

cd "$path/Data/"
save CCESUsable.dta, replace
